package net.maku.kyy.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.maku.framework.common.utils.PageResult;
import net.maku.framework.common.utils.Result;
import net.maku.framework.operatelog.annotations.OperateLog;
import net.maku.framework.operatelog.enums.OperateTypeEnum;
import net.maku.kyy.dto.KyyImageReportDTO;
import net.maku.kyy.entity.KyyReportEntity;
import net.maku.kyy.service.KyyReportService;
import net.maku.kyy.query.KyyReportQuery;
import net.maku.kyy.utils.FileUploadUtil;
import net.maku.kyy.vo.KyyReportVO;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import jakarta.validation.Valid;

import java.io.IOException;
import java.util.List;

/**
 * 举报信息
 *
 * @author 智 2445219557@qq.com
 */
@RestController
@RequestMapping("/kyy/report")
@Tag(name="举报信息")
@AllArgsConstructor
@Slf4j
public class KyyReportController {

    @Autowired
    private FileUploadUtil fileUploadUtil;

    private final KyyReportService kyyReportService;

    @GetMapping("page")
    @Operation(summary = "分页")
    @PreAuthorize("hasAuthority('kyy:report:page')")
    public Result<PageResult<KyyReportVO>> page(@ParameterObject @Valid KyyReportQuery query){
        PageResult<KyyReportVO> page = kyyReportService.page(query);

        return Result.ok(page);
    }


    @GetMapping("{id}")
    @Operation(summary = "信息")
    @PreAuthorize("hasAuthority('kyy:report:info')")
    public Result<KyyReportVO> get(@PathVariable("id") Long id){
        KyyReportVO data = kyyReportService.get(id);

        return Result.ok(data);
    }

    @PostMapping
    @Operation(summary = "保存")
    @OperateLog(type = OperateTypeEnum.INSERT)
    @PreAuthorize("hasAuthority('kyy:report:save')")
    public Result<String> save(@RequestBody KyyReportVO vo){
        kyyReportService.save(vo);

        return Result.ok();
    }

    @PutMapping
    @Operation(summary = "修改")
    @OperateLog(type = OperateTypeEnum.UPDATE)
    @PreAuthorize("hasAuthority('kyy:report:update')")
    public Result<String> update(@RequestBody @Valid KyyReportVO vo){
        kyyReportService.update(vo);

        return Result.ok();
    }

    @DeleteMapping
    @Operation(summary = "删除")
    @OperateLog(type = OperateTypeEnum.DELETE)
    @PreAuthorize("hasAuthority('kyy:report:delete')")
    public Result<String> delete(@RequestBody List<Long> idList){
        kyyReportService.delete(idList);

        return Result.ok();
    }


    @GetMapping("export")
    @Operation(summary = "导出")
    @OperateLog(type = OperateTypeEnum.EXPORT)
    @PreAuthorize("hasAuthority('kyy:report:export')")
    public void export() {
        kyyReportService.export();
    }



    /**
     * 帖子举报
     * @param kyyImageReportDTO
     * @return
     */
    @PostMapping("/imgupload")
    @Operation(summary = "图片上传")
    @OperateLog(type = OperateTypeEnum.OTHER)
    public Result<String> ImageReport(@ModelAttribute KyyImageReportDTO kyyImageReportDTO){
        try{
            //处理上传的图片
            List<String> imgUrls= fileUploadUtil.uploadFiles(kyyImageReportDTO.getReportImg(),kyyImageReportDTO.getImgname());
            System.out.println("imgUrls = " + imgUrls);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return Result.ok();
    }

    /**
     * 帖子举报
     * @param kyyReportEntity
     * @return
     */
    @PostMapping("/post")
    @Operation(summary = "新增帖子举报")
    @OperateLog(type = OperateTypeEnum.INSERT)
    public Result<String> PostReport(@RequestBody KyyReportEntity kyyReportEntity){
//        log.info("密码登录：{}",kyyUserPasswordLoginDTO);
        boolean result =kyyReportService.InsertReport(kyyReportEntity);
        if (result){
            return Result.ok("举报成功");
        }
        return Result.error("举报失败");
    }

    /**
     * 评论举报
     * @param kyyReportEntity
     * @return
     */
    @PostMapping("/comment")
    @Operation(summary = "新增评论举报")
    @OperateLog(type = OperateTypeEnum.INSERT)
    public Result<String> CommentReport(@RequestBody KyyReportEntity kyyReportEntity){
        boolean result =kyyReportService.InsertReport(kyyReportEntity);
        if (result){
            return Result.ok("举报成功");
        }
        return Result.error("举报失败");
    }

//    /**
//     * 举报查询
//     * @param kyyReportEntity
//     * @return
//     */
//    @PostMapping("/query")
//    @Operation(summary = "查询举报信息")
//    @OperateLog(type = OperateTypeEnum.GET)
//    public Result<List<KyyReportEntity>> QueryReport(@RequestBody KyyReportEntity kyyReportEntity){
//        List<KyyReportEntity> list = kyyReportService.SelectReport(kyyReportEntity);
//        System.out.println("list = " + list);
//        return Result.ok(list);
//    }
}